﻿<?php
/**
 * Created by PhpStorm.
 * User: yinnannan
 * Date: 16/1/19
 * Time: 上午5:15
 */


require('application/classes/api/userassert.php');
class recharge  {

    public function insert($parm){
        $message = null;
        try {
            Database::instance()->begin();
            DB::insert("sysrecharge",array('Out_trade_no','money','status','model','come','orderId','userId'))
                ->values(array($parm['Out_trade_no'],$parm['money'],$parm['status'],$parm['model'],$parm['come'],$parm['orderId'],$parm['userId']))
                ->execute();
            $message['data'] = '订单创建成功';
            $message['msg']  =true;
        }catch (Exception $e){
            $message['data'] = $e->getMessage();
            $message['msg']  =false;
            Database::instance()->rollback();
        }finally{
            Database::instance()->commit();
        }
        return $message;
    }


    public function update($parm){
$message = null;
        try {
            Database::instance()->begin();
            $result = DB::select('*')->from('sysrecharge')->where('Out_trade_no','=',$parm['Out_trade_no'])->execute()->as_array();
            if(count($result)==0){
                return ;
            }
            if($result [0]['status']=='未充值' ){
                DB::update("sysrecharge")->set(array('status'=>'支付成功'))->where('Out_trade_no','=',$parm['Out_trade_no'])->execute();
                $parm['model']='充值模块';
                $parm['payDate'] = $result[0]['createDate'];
                $parm['userId'] =$result[0]['userId'];
                $parm['come'] = '微信';
                $parm['money'] = $result[0]['money'];
                DB::insert("sysrecorduser",array('Out_trade_no','money','model','come','userId'))
                    ->values(array($parm['Out_trade_no'],$parm['money'],$parm['model'],$parm['come'],$parm['userId']))
                    ->execute();

                //
                $userassert = new userassert();
                $userassert->createPay($parm);
                $parm['content']='您充值'.$parm['money'].'元成功到账，请检查您的充值账户';
                $parm['model'] ='充值消息';
                $parm['status'] = '未读';
                $parm['title' ]='充值消息';
                DB::insert("sysmessage",array('title','content','status','model','userId'))
                    ->values(array($parm['title'],$parm['content'],$parm['status'],$parm['model'],$parm['userId']))
                    ->execute();
		
            }

            Database::instance()->commit();
        }catch (Exception $e){
            $message['data'] = $e->getMessage();
            $message['msg']  =false;
            Database::instance()->rollback();
        }finally{
            echo json_decode($message);
        }
    }
   
}